package com.zhanghaijun.createCode.jira;

import java.lang.System;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import com.atlassian.jira.rest.client.IssueRestClient;
import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.ProjectRestClient;
import com.atlassian.jira.rest.client.SearchRestClient;
import com.atlassian.jira.rest.client.domain.BasicIssue;
import com.atlassian.jira.rest.client.domain.BasicProject;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.SearchResult;
import com.atlassian.jira.rest.client.domain.User;
import com.atlassian.jira.rest.client.domain.input.FieldInput;
import com.atlassian.jira.rest.client.domain.input.IssueInput;
import com.atlassian.jira.rest.client.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.domain.input.TransitionInput;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;
import com.zhanghaijun.createCode.util.PrintUtil;

/**
 * @author qianchengzhang
 */
public class Jira {

    static JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
    static NullProgressMonitor         pm      = new NullProgressMonitor();
    static String                      uri     = "http://jira.51.nb/";
    static String                      user    = "monitor";
    static String                      pwd     = "qwertypoiuyt";

    public static void main(String[] args) throws URISyntaxException {
        // TODO Auto-generated method stub
        Jira jira = new Jira();
        //jira.getIssue("TEST-4");
        jira.getAllProjectAndIssue();
    }

    /**
     * http://jira.51.nb/rest/api/2/search?jql=project=CREDIT
     * @param issueKey
     * @throws URISyntaxException
     */
    public static void getIssue(String issueKey) throws URISyntaxException {

        URI jiraServerUri = new URI(uri);
        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, user, pwd);
        // get issue through issueKey
        Issue issue = restClient.getIssueClient().getIssue(issueKey, pm);
        PrintUtil.println(issue.getSummary());

        // grab user info
        User usr = restClient.getUserClient().getUser(user, pm);
        System.out.println(usr);
        System.out.println(issue);
    }

    public static void getAllProjectAndIssue() throws URISyntaxException {
        URI jiraServerUri = new URI(uri);
        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, user, pwd);
        ProjectRestClient prc = restClient.getProjectClient();
        IssueRestClient irc = restClient.getIssueClient();
        SearchRestClient src = restClient.getSearchClient();
        //获得所有的项目列表
        Iterable<BasicProject> listbp = prc.getAllProjects(pm);
        for (BasicProject bp : listbp) {

            //分项目查找
            System.out.println("project=" + bp.getKey());
            if("CREDIT".equals(bp.getKey())){
                //SearchResult sr = src.searchJql("project=" + bp.getKey(), pm);
                SearchResult sr = src.searchJql("project=" + bp.getKey(), 1000, 0, pm);

                //			System.out.println(sr);
                //遍历查找结果中的问题集合
                Iterable<BasicIssue> listi = sr.getIssues();
                int i =0;
                for (BasicIssue bi : listi) {
                    i=i+1;
                }
                System.out.println("get data....."+i);
            }

//            SearchResult sr = src.searchJql("project=" + bp.getKey(), pm);
//
//
//
//            //			System.out.println(sr);
//            //遍历查找结果中的问题集合
//            Iterable<BasicIssue> listi = sr.getIssues();
//            for (BasicIssue bi : listi) {
//                				//System.out.println(bi);
//                //获取IssueKey 查找完整Issue
//                if("TEST-4".equals(bi.getKey())){
//                    System.out.println(bp);
//                    System.out.println(bi);
//                    System.out.println(irc.getIssue(bi.getKey(),pm));
//                }
//            }
        }
    }

    /*
     * create the issue 
     * @param Issue type : 1L:bug; 2L:new requirement; 3L:task; 4L.improvement 
     * @param projectName:such as "GTP",the project short name 
     * @param Longid:such as 10000 
     * @param Summary:topic 
     * @param Description:
     */
    public static void createIssue(String projectName, String issueType, String description, String summary)
                                                                                                            throws URISyntaxException {
        // JerseyJiraRestClientFactory factory = new
        // JerseyJiraRestClientFactory();
        URI jiraServerUri = new URI(uri);
        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, user, pwd);
        IssueInputBuilder issueBuilder = new IssueInputBuilder("ProjectKey", Long.valueOf(issueType));

        final URI projectUri = new URI("http://jira.51nb.top/rest/api/2/project/" + projectName);
        BasicProject bporject = new BasicProject(projectUri, projectName, "");
        issueBuilder.setProject(bporject);
        issueBuilder.setDescription(description);
        issueBuilder.setSummary(summary);

        IssueInput issueInput = issueBuilder.build();
        BasicIssue bIssue = restClient.getIssueClient().createIssue(issueInput, pm);
        // print the newly created issuekey
        System.out.println(bIssue.getKey()); 
        System.out.println("ok"); 
    }

    public static void updateIssueField(String key, String field, String value) throws URISyntaxException {

        URI jiraServerUri = new URI(uri);
        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, user, pwd);
        // get the issue through issuekey
        Issue issue = restClient.getIssueClient().getIssue(key, pm);
        // update the default field environment,if the field is defined by
        FieldInput fieldinput = new FieldInput("environment", value);
        List<FieldInput> fields = new ArrayList<FieldInput>();
        fields.add(fieldinput);
        /**
         * @todo 更新issue
         */
        //		restClient.getIssueClient().update(issue, fields, pm);
    }

    public static void changeIssueStatus(String issuekey) throws URISyntaxException {
        URI jiraServerUri = new URI(uri);
        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, user, pwd);
        Issue issue = restClient.getIssueClient().getIssue(issuekey, pm);
        //the number 3 is involed below picture,you can focus on the red rectangular
        TransitionInput tinput = new TransitionInput(3);
        restClient.getIssueClient().transition(issue, tinput, pm);
    }

}